<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>电话号码检测</title>
    <script>
        /*
            要求：电话号码：7/8
                5188581
                51885810
                0550-5188581
                (0550)5188581
                0550-51885810
                (0550)51885810
                025-5188581
                025-51885810
        */
        
        function $(ele){ return document.querySelector(ele) }
        window.onload = function(){
            var reg = /^((0\d{2,3}-)|(\(0\d{2,3}\)))?([1-9]\d{6,7})$/;
    
            $('.txt').onkeyup = function(){
                if(reg.test(this.value)){
                    $('.text').innerText = 'OK'
                }else{
                    $('.text').innerText = 'NO'
                }
            }
        }
        </script>

</head>
<body>
    <input type="text" class="txt"><span class="text"></span>
</body>
</html>